package com.ufo.store.management.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.utils.GlideUtils;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.bean.EZCameraInfo;

import java.util.List;

/**
 *  作者：taoli on 2017/7/4 0004 16:31
 *  
 */
public class CameraRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<EZCameraInfo> mCameraInfoList = null;
    private int lastPosition = -1;

    public CameraRecyclerViewAdapter(Context context, List<EZCameraInfo> deviceInfoList) {
        this.context = context;
        mCameraInfoList = deviceInfoList;
    }

    public void setData(List<EZCameraInfo> list) {
        this.mCameraInfoList = list;
    }

    public EZCameraInfo getItem(int pos) {
        EZCameraInfo info = mCameraInfoList.get(pos);
        return info;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mCameraInfoList.size();
    }

    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    /**
     * @desc 自定义动画
     * @author taoli
     * @time 2017/6/9 0009
     */
    private void setAnimation(View viewToAnimate, int position) {
        if (position > lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, 100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        } else if (position < lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, -100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        }
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public ImageView coverIv;
        public ImageView cameraStatusIv;
        public TextView cameraLevelTv;
        public ImageView deviceIconIv;
        public TextView cameraNameTv;
        public TextView cameraNoTv;

        public RecyclerViewHolder(View view) {
            super(view);
            coverIv = (ImageView) view.findViewById(R.id.item_icon);
            cameraStatusIv = (ImageView) view.findViewById(R.id.camera_status_iv);
            cameraLevelTv = (TextView) view.findViewById(R.id.camera_level_tv);
            deviceIconIv = (ImageView) view.findViewById(R.id.device_icon_iv);
            cameraNameTv = (TextView) view.findViewById(R.id.camera_name_tv);
            cameraNoTv = (TextView) view.findViewById(R.id.camera_no_tv);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.camera_recycleritem, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final EZCameraInfo cameraInfo = mCameraInfoList.get(position);
        if (cameraInfo != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;

                holder1.cameraStatusIv.setVisibility(View.GONE);
                holder1.cameraNameTv.setText(cameraInfo.getCameraName());
                holder1.cameraNoTv.setText("通道" + String.valueOf(cameraInfo.getCameraNo()));
                holder1.cameraNoTv.setVisibility(View.GONE);

                // 视频质量，2-高清，1-标清，0-流畅
                if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_FLUNET) {
                    holder1.cameraLevelTv.setText("流畅");
                } else if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_BALANCED) {
                    holder1.cameraLevelTv.setText("平衡");
                } else if (cameraInfo.getVideoLevel() == EZConstants.EZVideoLevel.VIDEO_LEVEL_HD) {
                    holder1.cameraLevelTv.setText("高清");
                }

                String imgUrl = cameraInfo.getCameraCover();
                if (!TextUtils.isEmpty(imgUrl)) {
                    GlideUtils.LoadImage(context, imgUrl, holder1.coverIv);
                }

//                if (cameraInfo.getDeviceSerial().contains("497176528")) {
//                    holder1.deviceIconIv.setImageResource(R.drawable.device_pic_8_16_nvr);
//                } else if (cameraInfo.getDeviceSerial().contains("648447031")) {
//                    holder1.deviceIconIv.setImageResource(R.drawable.device_r1);
//                } else if (cameraInfo.getDeviceSerial().contains("485898386")) {
//                    holder1.deviceIconIv.setImageResource(R.drawable.device_h2s);
//                }
//
//                if (cameraInfo.getCameraName().contains("1号收银台")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic1, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("2号收银台")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic2, holder1.coverIv, defaultOptions);
//                }  else if (cameraInfo.getCameraName().contains("店仓库")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic3, holder1.coverIv, defaultOptions);
//                }  else if (cameraInfo.getCameraName().contains("店门口")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic4, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频5@DS-7808HW")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic5, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频6@DS-7808HW")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic6, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频7@DS-7808HW")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic7, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频8@DS-7808HW")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic8, holder1.coverIv, defaultOptions);
//                }
//                if (cameraInfo.getCameraName().contains("C2(485898386)")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.c2, holder1.coverIv, defaultOptions);
//                }
//                if (cameraInfo.getCameraName().contains("视频1@DS-7104N-SN")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic11, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频2@DS-7104N-SN")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic12, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频3@DS-7104N-SN")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic13, holder1.coverIv, defaultOptions);
//                } else if (cameraInfo.getCameraName().contains("视频4@DS-7104N-SN")) {
//                    ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pic14, holder1.coverIv, defaultOptions);
//                }
            }
        }

        //进入动画
        setAnimation(holder.itemView, position);

        //点击事件
        if (mListener != null)

        {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(v, pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }
    }

    private OnClickListener mListener = null;

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
}
